#include "StdAfx.h"
#include "NumSelect.h"
#include "resource.h"


BEGIN_MESSAGE_MAP(CNumSelect, CStatic)
    ON_WM_PAINT()
    ON_WM_LBUTTONUP()
END_MESSAGE_MAP()


CNumSelect::CNumSelect(void)
{
    mn_max_count = 6;
    mab_active.SetSize(6);
    for (int t=0;t<6;t++)
        mab_active[t] = false;

    mn_cur_select = -1;
}

CNumSelect::~CNumSelect(void)
{
}

void CNumSelect::SetCount(int n_max)
{
    mn_max_count = n_max;

    INT_PTR olds = mab_active.GetCount();
    mab_active.SetSize(n_max);
    while (olds<n_max) mab_active[olds++] = false;

    if (mn_cur_select>=n_max) mn_cur_select = -1;

    Invalidate();
}


int  CNumSelect::GetCurSel()
{
    return mn_cur_select;
}

//////////////////////////////////////////////////////////////////////////
void CNumSelect::SetCurSel(int i)
{
    ASSERT(i>=-1 && i<mab_active.GetCount());
    if (i>=-1 && i<mab_active.GetCount() && mn_cur_select != i)
    {
        mn_cur_select = i;
        Invalidate();
    }
}


//////////////////////////////////////////////////////////////////////////
void CNumSelect::SetUsed(int i, bool b_in_use /*= true*/)
{
    ASSERT(i>=-1 && i<mab_active.GetCount());
    if (i>=0 && i<mab_active.GetCount())
    {
        if (mab_active[i] != b_in_use)
        {
            mab_active[i] = b_in_use;
            Invalidate();
        }
    }
}


void CNumSelect::SetUsed(bool b_in_use /*= true*/)
{
    ASSERT(mn_cur_select>=-1 && mn_cur_select<mab_active.GetCount());
    if (mn_cur_select>=0 && mn_cur_select<mab_active.GetCount())
    {
        if (mab_active[mn_cur_select] != b_in_use)
        {
            mab_active[mn_cur_select] = b_in_use;
            Invalidate();
        }
    }
}



//////////////////////////////////////////////////////////////////////////
void CNumSelect::PreSubclassWindow()
{
    // Maik Jurkait [11/15/2006]
    // ------------------------
    // loading timer image-list
    // ------------------------
    CBitmap bitmap;
    bitmap.LoadBitmap(IDB_TIMERS);
    mc_list.Create(12,12,ILC_COLOR24 ,14,4);
    mc_list.Add(&bitmap,RGB(255,255,255));
    bitmap.DeleteObject();

    CStatic::PreSubclassWindow();
}



//////////////////////////////////////////////////////////////////////////
void CNumSelect::OnPaint()
{
    CPaintDC dc(this); // device context for painting

    CRect r;
    GetClientRect(r);

    dc.FillSolidRect(r,GetSysColor(COLOR_BTNFACE));

    int space = (r.Width()-4) / mn_max_count;
    int base = (r.Height()-12)/2;

    for (int t=0;t<mn_max_count;t++)
    {
        mc_list.Draw(&dc,t + (mab_active[t]?0:7),CPoint(2+space*t,base),ILD_NORMAL);
    }

    if (mn_cur_select>=0)
    {
        dc.Draw3dRect(space*mn_cur_select+1,base,14,12,RGB(255,0,0),RGB(255,0,0));
        dc.Draw3dRect(space*mn_cur_select,base+1,16,10,RGB(255,0,0),RGB(255,0,0));
    }
}


//////////////////////////////////////////////////////////////////////////
void CNumSelect::OnLButtonUp(UINT nFlags, CPoint point)
{
    CRect r;
    GetClientRect(r);
    int space = r.Width() / mn_max_count;

    int hitpoint = (point.x + (space-12)/2)/space;

    if (hitpoint != mn_cur_select || (nFlags&MK_SHIFT)!=0)
    {
        mn_cur_select = hitpoint;
        GetParent()->SendMessage(WM_NUMCHANGE,
                                 MAKEWPARAM(GetDlgCtrlID(),0),
                                 MAKELPARAM(mn_cur_select,(nFlags&MK_SHIFT)?1:0));
        Invalidate();
    }


    CStatic::OnLButtonUp(nFlags, point);
}


